//
//  MDDehumidifierController.m
//  MideaWorld_Zizhu
//
//  Created by Minus on 15/8/25.
//  Copyright (c) 2015年 Minus. All rights reserved.
//

#import "MDDehumidifierController.h"
#import "MDXPGApplianceApi.h"
#import "IDataBodyDeHumiStatus.h"
#import "DeHumidification.h"
#import "DataBodyDeHumiRequest.h"
#import "MDUserDefaults.h"

@interface MDDehumidifierController ()
{
    DataBodyDeHumiRequest *cmdData;
    IOSByteArray *order;
}

@property (weak, nonatomic) IBOutlet UISwitch *powerSwitch;
@property (weak, nonatomic) IBOutlet UISegmentedControl *modeSegment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *windSpeedSegment;
@property (weak, nonatomic) IBOutlet UITextField *humidityTextField;
@property (weak, nonatomic) IBOutlet UITextView *resultTextView;

@end

@implementation MDDehumidifierController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)actionQuery:(id)sender
{
    FactoryDataBody *query = (FactoryDataBody *)[FactoryDataBody queryRequestWithInt:FactoryDataBody_get_Dehumidifier()];
    order = [query toBytes];
    
    self.resultTextView.text = @"正在发送命令...";
    
    [[MDXPGApplianceApi getInstance] transparentCmd:[MDUserDefaults sharedInstance].currentApplianceID order:order completionForDehumidifier:^(BaseMessage *baseMessage, NSString *reply, DeHumidification *status) {
        
        if ([baseMessage getCode] == 0)
        {
            [self.resultTextView insertText:@"命令发送成功..."];
            [self.resultTextView insertText:[status description]];
        }
        else
        {
            [self.resultTextView insertText:@"命令发送失败..."];
            [self.resultTextView insertText:[baseMessage getMessage]];
        }
    }];
}

- (IBAction)actionSendCmd:(id)sender
{
    // 除湿器Model
    DeHumidification *dehumi = [DeHumidification new];
    
    // 开关
    dehumi->powerMode_ = self.powerSwitch.on ? 1 : 0;
    
    // 模式
    switch (self.modeSegment.selectedSegmentIndex)
    {
        case 0:
            dehumi->setMode_ = IDataBodyDeHumiStatus_MODE_A1_SMART;   // 智能
            break;
            
        case 1:
            dehumi->setMode_ = IDataBodyDeHumiStatus_MODE_A1_CONT;    // 连续
            break;
            
        case 2:
            dehumi->setMode_ = IDataBodyDeHumiStatus_MODE_A1_SET;     // 设定
            break;
            
        default:
            break;
    }
    
    // 风速
    switch (self.windSpeedSegment.selectedSegmentIndex)
    {
        case 0:
            dehumi->windSpeed_ = IDataBodyDeHumiStatus_WIND_SPEED_LOW;    // 低风
            break;
            
        case 1:
            dehumi->windSpeed_ = IDataBodyDeHumiStatus_WIND_SPEED_HIGH;   // 高风
            break;
            
        default:
            break;
    }
    
    // 湿度
    dehumi->humidity_set_ = [self.humidityTextField.text intValue];
    
    
    cmdData = (DataBodyDeHumiRequest *)[FactoryDataBody cmdRequestWithInt:FactoryDataBody_get_Dehumidifier()];
    [cmdData setDataBodyStatusWithBaseDevice:dehumi];
    order = [cmdData toBytes];
    
    self.resultTextView.text = @"正在发送命令...";
    
    [[MDXPGApplianceApi getInstance] transparentCmd:[MDUserDefaults sharedInstance].currentApplianceID order:order completionForDehumidifier:^(BaseMessage *baseMessage, NSString *reply, DeHumidification *status) {
        
        if ([baseMessage getCode] == 0)
        {
            [self.resultTextView insertText:@"命令发送成功..."];
            [self.resultTextView insertText:[status description]];
        }
        else
        {
            [self.resultTextView insertText:@"命令发送失败..."];
            [self.resultTextView insertText:[baseMessage getMessage]];
        }
    }];
}

@end
